﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITJZ.BitHelper.APIModel;
using System.Net;
using ITJZ.BitHelper.Local.UI;
using System.Xml;
using ITJZ.BitHelper.Tools;

namespace ITJZ.BitHelper.Local.API
{
    public class Knowledge : IAPI.IKnowledge
    {
        /// <summary>
        /// 重载接口中包含的Add方法，方便客户端使用
        /// </summary>
        /// <param name="model"></param>
        public ResInfo Add(APIModel.Knowledge model)
        {
            ResInfo res = this.Add(model.Language, model.Library, model.FullName, model.Content);
            return res;
        }

        #region IKnowledge 成员

        public System.Xml.XmlDocument Menu()
        {
            string address = Config.ApiUrl + "?class=Knowledge&action=Menu&key=" + User.CurUser.StateKey;

            string reply = HttpHelper.GetString(address);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reply);
            return doc;
        }

        public System.Xml.XmlDocument LibList()
        {
            string address = Config.ApiUrl + "?class=Knowledge&action=LibList&key=" + User.CurUser.StateKey;
            string reply = HttpHelper.GetString(address);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reply);
            return doc;

        }

        public ITJZ.BitHelper.APIModel.ResInfo Add(string language, string library, string fullName, string content)
        {
            content = System.Web.HttpUtility.UrlEncode(content, Encoding.UTF8);//



            string address = Config.ApiUrl + "?class=Knowledge&action=Add&key=" + User.CurUser.StateKey;
            string post = string.Format("language={0}&library={1}&fullName={2}&content={3}",
                language,library,fullName,content);

            string reply = HttpHelper.UploadString(address, post);


            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reply);

            XmlNode root = doc.SelectSingleNode("ResInfo");

            ITJZ.BitHelper.APIModel.ResInfo resinfo = new ResInfo();
            resinfo.ErrState = int.Parse(root.SelectSingleNode("ErrState").InnerText);
            resinfo.Msg = root.SelectSingleNode("Msg").InnerText;


            if (resinfo.ErrState!=0)
            {
                User user = new User();
                user.Login();
            }

            return resinfo;

            
        }

        public List<ITJZ.BitHelper.APIModel.Knowledge> Search(string language, string library, string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                key = "%" + key.Trim() + "%";
            }

            if (string.IsNullOrEmpty(key))
            {
                key = "%%";
            }

            string address = Config.ApiUrl + "?class=Knowledge&action=Search&key=" + User.CurUser.StateKey +string.Format("&language={0}&library={1}&keywords={2}", language, library, key);

            string reply = HttpHelper.GetString(address );


            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reply);

            XmlNode root = doc.SelectSingleNode("ArrayOfKnowledge");

            List<ITJZ.BitHelper.APIModel.Knowledge> list = new List<ITJZ.BitHelper.APIModel.Knowledge>();
            XmlNodeList nodelist = root.SelectNodes("Knowledge");
            foreach (XmlNode nono in nodelist)
            {
                ITJZ.BitHelper.APIModel.Knowledge temp = new ITJZ.BitHelper.APIModel.Knowledge();
                //temp.Content = nono.SelectSingleNode("Content").InnerXml;
                temp.CreateTime =DateTime.Parse( nono.SelectSingleNode("CreateTime").InnerXml);
                temp.FullName = nono.SelectSingleNode("FullName").InnerXml;
                temp.Guid = nono.SelectSingleNode("Guid").InnerXml;
                temp.Language = nono.SelectSingleNode("Language").InnerXml;
                temp.Library = nono.SelectSingleNode("Library").InnerXml;
                temp.ShowTime = int.Parse(nono.SelectSingleNode("ShowTime").InnerXml);
                temp.Uid = nono.SelectSingleNode("Uid").InnerXml;
                temp.Vote = int.Parse(nono.SelectSingleNode("Vote").InnerXml);

                list.Add(temp);
            }

            return list;
        }

        public ITJZ.BitHelper.APIModel.Knowledge GetKnowledge(string guid)
        {
            string address = Config.ApiUrl + "?class=Knowledge&action=GetKnowledge&guid=" + guid + "&key=" + User.CurUser.StateKey; ;
            string reply = HttpHelper.GetString(address);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reply);

            ITJZ.BitHelper.APIModel.Knowledge knowledge = new ITJZ.BitHelper.APIModel.Knowledge();

            XmlNode root = doc.SelectSingleNode("Knowledge");
            knowledge.Content = root.SelectSingleNode("Content").InnerXml;
            knowledge.CreateTime = DateTime.Parse(root.SelectSingleNode("CreateTime").InnerXml);
            knowledge.FullName = root.SelectSingleNode("FullName").InnerXml;
            knowledge.Guid = root.SelectSingleNode("Guid").InnerXml;
            knowledge.Language = root.SelectSingleNode("Language").InnerXml;
            knowledge.Library = root.SelectSingleNode("Library").InnerXml;
            knowledge.ShowTime = int.Parse(root.SelectSingleNode("ShowTime").InnerXml);
            knowledge.Uid = root.SelectSingleNode("Uid").InnerXml;
            knowledge.Vote = int.Parse(root.SelectSingleNode("Vote").InnerXml);

            return knowledge;
        }

        public ITJZ.BitHelper.APIModel.ResInfo Vote(string knowledgeGuid, int score)
        {
            string address = Config.ApiUrl + "?class=Knowledge&action=GetKnowledge&knowledgeGuid=" + knowledgeGuid + "&score=" + score.ToString() + "&key=" + User.CurUser.StateKey;
            string reply = HttpHelper.GetString(address);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reply);


            ITJZ.BitHelper.APIModel.ResInfo resinfo = new ResInfo();
            resinfo.ErrState = int.Parse(doc.SelectSingleNode("ErrState").Value);
            resinfo.Msg = doc.SelectSingleNode("Msg").Value;

            return resinfo;
        }

        #endregion
    }
}
